<?php

namespace app\admin\validate\user;

use app\common\model\user\UserGroup;
use tmcore\base\BaseValidate;

/**
 * 分组验证器
 */
class GroupValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|checkGroup',
        'name' => 'require|length:1,30',
        'is_default' => 'require|in:0,1',
        'sort' => 'egt:0',
    ];

    protected $message = [
        'id.require' => 'id不能为空',
        'name.require' => '名称不能为空',
        'name.length' => '名称长度须在1-30位字符',
        'is_default.require' => '默认值不能为空',
        'is_default.in' => '默认值参数有误',
        'sort.egt' => '排序值不正确',
    ];

    /**
     * 添加场景
     * @return GroupValidate
     */
    public function sceneAdd()
    {
        return $this->remove(['id'])
            ->remove('id','require|checkGroup');
    }

    /**
     * 详情场景
     * @return GroupValidate
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    /**
     * 更改状态场景
     * @return GroupValidate
     */
    public function sceneStatus()
    {
        return $this->only(['id', 'status']);
    }

    /**
     * 编辑场景
     * @return GroupValidate
     */
    public function sceneEdit()
    {
        return $this;
    }

    /**
     * 删除场景
     * @return GroupValidate
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }

    /**
     * 检查分组是否存在
     * @param $value
     * @return bool|string
     */
    public function checkGroup($value)
    {
        $article = UserGroup::findOrEmpty($value);
        if ($article->isEmpty()) {
            return '分组不存在';
        }
        return true;
    }
}